package com.ruoyi.rycc.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.rycc.domain.CcSubject;
import com.ruoyi.rycc.service.ICcSubjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 专题管理Controller
 *
 * @author antg
 * @date 2021-08-28
 */
@Controller
@RequestMapping("/subject/subject")
public class CcSubjectController extends BaseController {
    private String prefix = "subject/subject";

    @Autowired
    private ICcSubjectService ccSubjectService;

    @RequiresPermissions("subject:subject:view")
    @GetMapping()
    public String subject() {
        return prefix + "/subject";
    }

    /**
     * 查询专题管理列表
     */
    @RequiresPermissions("subject:subject:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CcSubject ccSubject) {
        startPage();
        List<CcSubject> list = ccSubjectService.selectCcSubjectList(ccSubject);
        return getDataTable(list);
    }

    /**
     * 导出专题管理列表
     */
    @RequiresPermissions("subject:subject:export")
    @Log(title = "专题管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CcSubject ccSubject) {
        List<CcSubject> list = ccSubjectService.selectCcSubjectList(ccSubject);
        ExcelUtil<CcSubject> util = new ExcelUtil<CcSubject>(CcSubject.class);
        return util.exportExcel(list, "专题管理数据");
    }

    /**
     * 新增专题管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存专题管理
     */
    @RequiresPermissions("subject:subject:add")
    @Log(title = "专题管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, CcSubject ccSubject) throws IOException {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        ccSubject.setSubjectBanner(fileName);
        return toAjax(ccSubjectService.insertCcSubject(ccSubject));
    }

    /**
     * 修改专题管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {

        CcSubject ccSubject = ccSubjectService.selectCcSubjectById(id);
        mmap.put("ccSubject", ccSubject);
        return prefix + "/edit";
    }

    /**
     * 修改保存专题管理
     */
    @RequiresPermissions("subject:subject:edit")
    @Log(title = "专题管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam(value = "file", required = false) MultipartFile file, CcSubject ccSubject) throws IOException {
        if (file != null) {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            ccSubject.setSubjectBanner(fileName);
        }
        return toAjax(ccSubjectService.updateCcSubject(ccSubject));
    }

    /**
     * 删除专题管理
     */
    @RequiresPermissions("subject:subject:remove")
    @Log(title = "专题管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ccSubjectService.deleteCcSubjectByIds(ids));
    }
}
